package cn.gqr.controller;

import cn.gqr.entity.Largefile;
import cn.gqr.entity.User;
import cn.gqr.service.HotService;
import cn.gqr.service.UserService;
import cn.gqr.tool.GetUser;
import cn.gqr.tool.RespJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

//用户
@RestController
@RequestMapping("/api")
public class UserController {

    @Autowired
    UserService userService;
    @Autowired
    HotService hotService;

//前台

    //更新密码
    @RequestMapping("/fore/user/updatePwd")
    public Map<String, Object> updatePwd(String pwd){
        System.out.println(pwd);
        int i = userService.updatePwd(pwd, ((User) GetUser.getUser()).getId());
        return RespJson.pack(200,"密码更新成功...",i);
    }

    //更新用户头像
    @RequestMapping("/fore/user/updateImg")
    public Map<String, Object> updateImg(@RequestParam(value = "pic",required = false)MultipartFile file, Largefile largefile){
        if(file==null){
            return RespJson.pack(404, "头像未更新", null);
        }
        int uid = ((User) GetUser.getUser()).getId();
        //更新头像
        String uuid = userService.updateImg(uid, file, largefile);
        return RespJson.pack(200,"更新用户头像成功..",uuid);
    }

    //更新用户信息
    @RequestMapping("/fore/user/updateInfo")
    public Map<String, Object> updateInfo(String nickname, String phone){
        int i = userService.updateInfo(nickname, phone, ((User) GetUser.getUser()).getId());
        return RespJson.pack(200,"更新用户信息成功",i);
    }


//后台

    //全部买家信息
    @RequestMapping("/adm/user/findAll/{start}/{num}")
    public Map<String, Object> findAll(@PathVariable("start")int start, @PathVariable("num")int num){
        Map map = new HashMap();
        map.put("total",userService.userTotal());
        map.put("users",userService.findAll(start,num));
        return RespJson.pack(200,"",map);
    }

    //添加管理员
    @RequestMapping("/adm/user/addAdm")
    public Map<String, Object> addAdm(User user,@RequestParam(value = "pic",required = false)
            MultipartFile file, Largefile largefile){
        return userService.addAdm(user,file,largefile);
    }

    //重置密码 解冻
    @RequestMapping("/adm/user/resetPwd/{uid}")
    public Map<String, Object> resetPwd(@PathVariable("uid")int uid){
        int i = userService.resetPwd(uid);
        return RespJson.pack(200, "重置密码,解冻成功",i);
    }

    //冻结
    @RequestMapping("/adm/user/close/{uid}")
    public Map<String, Object> close(@PathVariable("uid")int uid){
        int close = userService.close(uid);
        return RespJson.pack(200, "冻结成功",close);
    }

    //id查询用户信息
    @RequestMapping("/adm/user/findById/{id}")
    public Map<String, Object> findById(@PathVariable("id")int id){
        return RespJson.pack(200, "",userService.findById(id));
    }


    @RequestMapping("/adm/user/findByIdUserInfo/{id}")
    public Map<String, Object> findByIdUserInfo(@PathVariable("id")int id){
        return RespJson.pack(200,"",userService.findByIdUserInfo(id));
    }


}
